package com.intel.two.controller.community;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.intel.two.bo.community.SecurityBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.listener.community.WebSecurityListener;
import com.intel.two.pojo.community.Security;
import com.intel.two.service.community.SecurityService;
import com.intel.two.vo.community.securityBarVo;
import com.intel.two.vo.community.securityPieVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/security")
public class SecurityController {
    @Autowired
    private SecurityService securityService;

    /*查全部+分页*/
    @RequestMapping("/securityList")
    public LayuiPageVo<Security> securityPageData(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return securityService.securityPageData(params);
    }

    /*添加*/
    @PostMapping("/addSecurity")
    public R addStudent(Security security) {
        System.out.println(security);
        int result = securityService.addSecurity(security);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /*删除*/
    @GetMapping("/deleteSecurity")
    public R deleteSecurity(Integer security_id) {

        int result = securityService.deleteSecurity(security_id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /*批量删除*/
    @GetMapping("/deleteSecurityAll")
    public R deleteSecurityAll(@RequestParam Map<String, Object> map) {

        int result = securityService.deleteSecurityAll(map);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /*更新*/
    @PostMapping("updateSecurity")
    public R updateSecurity(Security security) {
        System.out.println(security);
        int result = securityService.updateSecurity(security);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /*柱状图*/
    @RequestMapping("/securityBar")
    public R securityBar() {
        List<securityBarVo> list = securityService.sexBarData();
        return R.ok().data("sexBar", list);
    }
    /*饼状图*/
    @RequestMapping("/securityPie")
    public R securityPie() {
        List<securityPieVo> list = securityService.sexPieData();
        return R.ok().data("sexPic", list);
    }


    @Autowired
    private WebSecurityListener webSecurityListener;

    //读取excel文件  把excel 文件中的数据存入数据库
    @RequestMapping("/securityImport")
    public R importExcel(MultipartFile file) throws IOException {
        /**
         * 参数1  文件流
         * 参数2 读取到的数据封装到哪个类型
         * 参数3 监听器  每读取一行数据 都会调用监听器的 invoke 方法
         *
         * */

        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), SecurityBo.class, webSecurityListener);
        //得到 读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();

        return R.ok();
    }
    //模板下载
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");

        response.setCharacterEncoding("utf-8");
        String realPath = request.getServletContext().getRealPath("/template/保安信息模板.xlsx");
        File file = new File(realPath);
        //将文件转为一个字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        //防止中文乱码
        String filename = URLEncoder.encode("保安信息模板", "utf-8");
        //
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.close();
    }

    /*导出*/
    @RequestMapping("/securityExport")
    public void export(Security security, HttpServletResponse response) throws IOException {
        //文件的下载   关键点 要设置 响应头

        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("保安信息导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");


        // 得到 输出流 输出流向浏览器输出数据
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream,SecurityBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        List<SecurityBo> securityBos = securityService.securityExcelData(security);
        sheet.doWrite(securityBos);
    }


}